package cn.hefan.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.hefan.domain.Phone;
import cn.hefan.util.DataSourceUtils;

public class PhoneDao {

	public static List<Phone> SelectPhone() throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from phone";
		List<Phone> phones = qr.query(sql, new BeanListHandler<Phone>(Phone.class));
		return phones;
	}

	public static List<Phone> SelectPhone(String divide) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from phone where tri=?";
		List<Phone> phones = qr.query(sql, new BeanListHandler<Phone>(Phone.class),divide);
		return phones;
	}

	public static List<Phone> SelectName(String name) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from phone where name=?";
		List<Phone> phones = qr.query(sql, new BeanListHandler<Phone>(Phone.class),name);
		return phones;
	}

	public static int addPhone(Phone phone) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into phone(name,phone,pro,tri) values(?,?,?,?)";
		return qr.update(sql,phone.getName(),phone.getPhone(),phone.getPro(),phone.getTri());
	}

	public static int deletPhone(String id) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "delete from phone where id=?";
		return qr.update(sql,id);
	}

}
